#  halt.0.1, part of project GUIedCLI, a GUI of command 'shutdown'.
#  Copyright (C) 2010  exaatto
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 3, or (at your option)
#  any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, see <http://www.gnu.org/licenses/>.


import os
import wx
import string

class MainWindow(wx.MiniFrame):
	
	def __init__(self, parent, title):

		wx.MiniFrame.__init__(self, parent, title=title, pos=wx.DefaultPosition, size=(215,150), style=wx.DEFAULT_FRAME_STYLE)

		# option to pass to 'shutdown' cmd
		option = " "
		self.option = option
		time_delay = ' /t 0'
		self.time_delay = time_delay

		self.process = None

		# The frame has only a panel to hold the buttons and boxes
		panel = wx.Panel(self, -1)
		self.panel = panel

		# Checkboxes
		## force
		force = wx.CheckBox( panel, 1010, "&Force close running programe.", ( 10, 10))
		force.SetValue( False)
		self.Bind(wx.EVT_CHECKBOX, self.evt_force, force)
		self.force = force
		
		# time delay
		label = wx.StaticText(panel, -1, "Set time delay:", (15, 33))
		time_min = wx.TextCtrl(panel, -1, "0", size=(25, -1), style=wx.TE_RIGHT, pos=(105, 30))
		seperator = wx.StaticText(panel, -1, ":", (131, 32))
		time_sec = wx.TextCtrl(panel, -1, "0", size=(25, -1), style=wx.TE_RIGHT, pos=(135, 30))
		wx.CallAfter(time_min.SetInsertionPoint, 0)
		wx.CallAfter(time_sec.SetInsertionPoint, 0)
		self.time_sec = time_sec
		self.time_min = time_min

		# buttons
		shutdown = wx.Button(panel, 2010, "&Shutdown",( 20, 60))
		self.Bind(wx.EVT_BUTTON, self.Shutdown_on_click, shutdown)

		cancel_quit = wx.Button(panel, 2020, "&Quit", ( 105, 60))
		self.Bind(wx.EVT_BUTTON, self.cancel_on_click, cancel_quit)
		
		cancel_shutdown = wx.Button(panel, 2030, "&Cancel previous shutdown", ( 20, 88))
		self.Bind(wx.EVT_BUTTON, self.cancel_shutdown_on_click, cancel_shutdown)

	# CheckBox events
	## force
	def evt_force( self, e):
		if self.force.IsChecked() :
			self.option += " /f"
		else :
			self.option = self.option.replace( " /f", "")

	# buttons events
	def Shutdown_on_click( self, e):
	        
		# redir output from the cmd stdout
		self.process = wx.Process(self)
        	self.process.Redirect();
		
		self.time_delay = " /t " + str( int(self.time_min.GetValue())*60 + int(self.time_sec.GetValue()))
		wx.Execute("shutdown"+' /s'+self.option+self.time_delay, wx.EXEC_ASYNC, self.process)

	def cancel_on_click( self, e):
		self.Close( True)

	def cancel_shutdown_on_click( self, e) :
		# redir output from the cmd stdout
		self.process = wx.Process(self)
        	self.process.Redirect();

		wx.Execute("shutdown"+' /a', wx.EXEC_ASYNC, self.process)




		

##-------------------------------##
# main prog
app = wx.App(False)
main_win = MainWindow(None, "Shutdown")
main_win.Show(True)
app.MainLoop()


